# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=binutils

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.24
pkgrel=9
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('any')
url="http://www.gnu.org/software/binutils/"
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
#checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' '!distcc' '!ccache') # 'debug' '!strip')
#install=binutils.install
source=("ftp://mirrors.kernel.org/sources.redhat.com/${_realname}/releases/${_realname}-${pkgver}.tar.bz2"
    '010-2.23.52-install-libiberty.patch'
    '100-dont-escape-arguments-that-dont-need-it-in-pex-win32.c.patch'
    '020-add-bigobj-format.patch'
    '030-binutils-mingw-gnu-print.patch'
    '110-This-fixes-a-compile-time-warning.patch'
    '120-Bug-16858-weak-external-reference-has-wrong-value.patch'
    '130-make-relocbase-unsigned.patch')
md5sums=('e0f71a7b2ddab0f8612336ac81d9636b'
         '01590d2835f6eda9932f78f84718a1d9'
         'cc15baf6f7939980c7f406416736baa8'
         '62c646e3f8c4cb08d0916ab34faa8254'
         '6e8b708d437bccbfa46ba56421dabb40'
         '10fcfbca65c6939840dd0f4a575b8a99'
         '7b0d532ba0419ed2bdae746b1a4f42b6'
         '0513245c067162fa9f79b7e0befd201d')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/010-2.23.52-install-libiberty.patch
  [ -f gas/testsuite/gas/pe/big-obj.d ] && rm gas/testsuite/gas/pe/big-obj.d
  [ -f gas/testsuite/gas/pe/big-obj.s ] && rm gas/testsuite/gas/pe/big-obj.s
  patch -p1 -i "${srcdir}"/020-add-bigobj-format.patch
  patch -p1 -i "${srcdir}"/030-binutils-mingw-gnu-print.patch
  patch -p1 -i "${srcdir}"/100-dont-escape-arguments-that-dont-need-it-in-pex-win32.c.patch
# 110-This-fixes-a-compile-time-warning.patch isn't needed for 2.24 and may not
#  be needed after either; depending on what gets included in the next release.
#  patch -p1 -i "${srcdir}"/110-This-fixes-a-compile-time-warning.patch
# Fixes bug https://sourceware.org/bugzilla/show_bug.cgi?id=16858#c3
  patch -p1 -i "${srcdir}"/120-Bug-16858-weak-external-reference-has-wrong-value.patch

# I ran into a bug building Qt5Webkitd.dll
# "final link failed: File truncated"
# was due to a negative offset ending up in rel_filepos in _bfd_coff_final_link, this
# attempts to double the amount of room we have.
  patch -p1 -i "${srcdir}"/130-make-relocbase-unsigned.patch

  #do not install libiberty
  #sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  #sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  if [ "${CARCH}" = "x86_64" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  cd $srcdir
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-werror \
    --enable-lto \
    --with-sysroot=${MINGW_PREFIX} \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    $_conf \
    --disable-nls \
    --disable-rpath \
    --disable-multilib \
    --enable-install-libiberty

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{lib,include}/${_realname}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include
  cp ${pkgdir}${MINGW_PREFIX}/include/ansidecl.h ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/
  mv ${pkgdir}${MINGW_PREFIX}/include/*.h ${pkgdir}${MINGW_PREFIX}/include/${_realname}
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/${_realname}

  # Remove unwanted files
  # rm -rf ${pkgdir}${MINGW_PREFIX}/share

  # Add some useful headers
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/libiberty.h ${pkgdir}${MINGW_PREFIX}/include
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/demangle.h ${pkgdir}${MINGW_PREFIX}/include
}
